/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package engine.entities.inventory.item;

import engine.entities.Player;
import engine.entities.inventory.item.MobEquipSet.EQUIP_SLOT;
import engine.resource.Resources;

/**
 *
 * @author NathanT
 */
public class LeftSideTable extends InventoryTable {
    
    public static final String PRIMARY_HAND_ICON_KEY = "PRIMARY_HAND_SLOT_ICON";
    public static final String SECONDARY_HAND_ICON_KEY = "SECONDARY_HAND_SLOT_ICON";
    public static final String CHEST_ICON_KEY = "CHEST_SLOT_ICON";
    public static final String LEGS_ICON_KEY = "LEGS_SLOT_ICON";
    

    private Player player;
    
    public LeftSideTable(Player player) {
        super(2, 2);
        this.setPlayer(player);
        this.setInventoryTableListener(new LeftSideTableListener());
    }

    @Override
    protected void initRenderers() {
        this.items[0][0] = new InventoryItemRenderer(null, Resources.getSprite(PRIMARY_HAND_ICON_KEY), EQUIP_SLOT.PRIMARY_HAND);
        this.items[1][0] = new InventoryItemRenderer(null, Resources.getSprite(SECONDARY_HAND_ICON_KEY), EQUIP_SLOT.SECONDARY_HAND);
        this.items[0][1] = new InventoryItemRenderer(null, Resources.getSprite(CHEST_ICON_KEY), EQUIP_SLOT.CHEST);
        this.items[1][1] = new InventoryItemRenderer(null, Resources.getSprite(LEGS_ICON_KEY), EQUIP_SLOT.LEGS);
    }
    
    public final void setPlayer(Player player) {
        this.player = player;
        if (player == null) {
            emptyTable();
            return;
        }
        
        MobEquipSet set = player.getEquipSet();
        Item item;
        
        item = set.getItem(EQUIP_SLOT.PRIMARY_HAND);
        if (item != null) {
            this.items[0][0].setInvItem(new InventoryItem(item, 1));
        }
        else {
            this.items[0][0].setInvItem(null);
        }
        
        item = set.getItem(EQUIP_SLOT.SECONDARY_HAND);
        if (item != null) {
            this.items[1][0].setInvItem(new InventoryItem(item, 1));
        }
        else {
            this.items[1][0].setInvItem(null);
        }
        
        item = set.getItem(EQUIP_SLOT.CHEST);
        if (item != null) {
            this.items[0][1].setInvItem(new InventoryItem(item, 1));
        }
        else {
            this.items[0][1].setInvItem(null);
        }
        
        item = set.getItem(EQUIP_SLOT.LEGS);
        if (item != null) {
            this.items[1][1].setInvItem(new InventoryItem(item, 1));
        }
        else {
            this.items[1][1].setInvItem(null);
        }
    }
    
    private void emptyTable() {
        this.items[0][0].setInvItem(null);
        this.items[1][0].setInvItem(null);
        this.items[0][1].setInvItem(null);
        this.items[1][1].setInvItem(null);
    }
    
    private class LeftSideTableListener implements InventoryTableListener {

        @Override
        public void itemChanged(int row, int column, InventoryItem newItem) {
            if (newItem != null) {
                player.getEquipSet().setItem(items[row][column].getSlot(), newItem.getItem());
            }
            else {
                player.getEquipSet().setItem(items[row][column].getSlot(), null);
            }
        }
        
    }
    
}
